/**
 * Created by wenchuan on 2018/5/5.
 */
define(function () {

    var self,
        app = win.app,
        userInfo,
        customersArg,
        cdata = {
            GWAK009: '',//客户分类
            GWAK010: '',//客户渠道
            GWAK018: '',//线人电话
            GWAK017: '',//线人姓名
            GWAK022: '',
            GWAK023: '',
            GWAK024: '',
            GWAK025: '',
            GWAK026: '',
            GWAK028: '',
            GWAK030: '',
            GWAK031: '',
            GWAK050: '',
            GWAK053: '',
            GWAK055: '',
            GWAKL003: '',
            GWAK054: '',//行业分类
            GWAK008:'',
            GWAKPLANT:''
        },
        provinces = [],
        citys = [],
        districts = [],
        industrys = [], //行业分类
        customerTypes = [], //客户分类
        customerFroms = [], //客户渠道
        sites=[],
        coins=[]
    ;

    var DistributorAdd = app.Module.extend({

        init: function () {
            self = this;
            self._super({}, win);
            userInfo = app.userInfos.sysUser;
            cdata.GWAK031 = userInfo.NAME;//原来ID改成NAME 经销商显示
            cdata.GWAK030 = userInfo.MOBILE;
            customersArg = app.pageParameters['distributoradd_page'];
        },
        checkPhone: function () {

            app.ajax({
                xurl: "api/wx/gwat100/checkCustomerPhone",
                loading: true,
                xdata: {
                    GWAK053: cdata.GWAK053
                },
                cb: function (result) {
                    if (result.ok) {
                        var count = result.data.count;
                        if (count > 0) {
                            app.alert({
                                msg: '负责人手机号已经存在'
                            });
                        } else {
                            self.subData();
                        }
                    }
                }
            });
        },
        subData: function () {
            app.ajax({
                xurl: "api/wx/gwat200/insertGwak",
                loading: true,
                xdata: cdata,
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '添加成功！',
                            ok: function () {
                                app.back(1);
                            }
                        });
                        customersArg.cb();
                    }
                }
            });
        },
        getProvince: function () {
            app.ajax({
                xurl: "api/wx/user/getProvinces",
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAK023 = datas[0].GWAD002;
                        cdata.GWAK022 = datas[0].GWAD003;
                        provinces.pushArray(datas);
                        self.getCity();
                    }
                }
            });
        },
        getCity: function () {
            app.ajax({
                xurl: "api/wx/user/getCitys",
                xdata: {
                    GWAD003: cdata.GWAK023
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if(datas.length>0){
                            cdata.GWAK024 = datas[0].GWAD002;
                            citys.pushArray(datas);
                            self.getDistricts();
                        }else{
                            citys.removeAll();
                            districts.removeAll();
                        }
                    }
                }
            });
        },
        getDistricts: function () {
            app.ajax({
                xurl: "api/wx/user/getDistricts",
                xdata: {
                    GWAD003: cdata.GWAK024
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            cdata.GWAK025 = datas[0].GWAD002;
                            districts.pushArray(datas);
                        }else{
                            districts.removeAll();
                        }
                    }
                }
            });
        },
        getIndustry: function () {
            app.ajax({
                xurl: "api/wx/user/getGwagDatas",
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAK054 = datas[0].GWAG001;
                        industrys.pushArray(datas);
                    }
                }
            });
        },
        getCustomerTypes: function () {
            app.ajax({
                xurl: "api/wx/gwat200/getGwaf",
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAK009 = datas[0].GWAF001;
                        customerTypes.pushArray(datas);
                    }
                }
            });
        },
        getCustomerFroms: function () {
            app.ajax({
                xurl: "api/wx/gwat200/getGwae",
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAK010 = datas[0].GWAE001;
                        customerFroms.pushArray(datas);
                    }
                }
            });
        },
        getSite:function(){
            app.ajax({
                xurl: "api/wx/gwat100/getSite",
                xdata: {
                    GWDE002 : "ERP账套"
                },
                cb: function (result) {
                    if (result.ok)
                        sites=result.data.data;
                }
            });
        },
        getCoin:function(){
            app.ajax({
                xurl: "api/wx/gwat100/getCoin",
                xdata: {},
                cb: function (result) {
                    if (result.ok)
                        coins=result.data.data;
                }
            });
        },
        _lazy: function () {
            this._super();
            self.getProvince();
            self.getIndustry();
            self.getCustomerTypes();
            self.getCustomerFroms();
            self.getSite();
            self.getCoin();
        },
        _ready: function () {
            new Vue({
                el: doc.querySelector('.mui-content'),
                data: {
                    sub: cdata,
                    provinces: provinces,
                    citys: citys,
                    districts: districts,
                    industrys: industrys, //行业分类
                    customerTypes: customerTypes, //客户分类
                    customerFroms: customerFroms //客户渠道
                },
                methods: {
                    provinceChange: function () {
                        self.getCity();
                    },
                    cityChange: function () {
                        self.getDistricts();
                    },
                    addData: function () {
                        if (!cdata.GWAKL003 || cdata.GWAKL003 == '') {
                            app.toast('经销商名称不能为空');
                            return;
                        }
                        if (!cdata.GWAK050 || cdata.GWAK050 == '') {
                            app.toast('负责人姓名不能为空');
                            return;
                        }
                        if (!cdata.GWAK053 || cdata.GWAK053 == '') {
                            app.toast('手机号不能为空');
                            return;
                        }
                        app.confirm({
                            msg: '是否保存经销商信息？',
                            ok: function () {
                                self.checkPhone();
                            }
                        });
                    }
                }
            });

            app.taps.call(win,[
                {
                    o:'showSite',
                    cb:function () {
                        var list=[];
                        sites.forEach(function (item, index) {
                            list.push({
                                NAME:item.GWDE003+"   "+item.GWDE004,
                                VALUE:item.GWDE003
                            })
                        })
                        app.openPage({
                            src: "base/selectPage.html",
                            args: {
                                cb: function (res) {
                                    cdata.GWAKPLANT = res.VALUE;
                                },
                                item: list
                            }
                        })
                    }
                },
                {
                    o:'showCoin',
                    cb:function (res) {
                        var list=[];
                        coins.forEach(function (item, index) {
                            list.push({
                                NAME:item.GWAC001
                            })
                        })
                        app.openPage({
                            src: "base/selectPage.html",
                            args: {
                                cb: function (res) {
                                    cdata.GWAK008 = res.NAME;
                                },
                                item: list
                            }
                        })
                    }
                }
            ])
        }
    });

    return new DistributorAdd();

});